package com.demo.servlet.entity;

import com.demo.servlet.Constants;
import com.demo.servlet.entity.*;
import net.ipip.ipdb.District;

import java.io.IOException;

public class CityIspInfo {
    private static District district;
    static {
        try {
            district = new District(Constants.IpFilePath);
        } catch (IOException e){
            e.printStackTrace();
        }
    }

    private String countryNmae;
    private String regionName;
    private String cityName;
    private String districtName;
    private String networkService;

    public CityIspInfo(){
    }

    public CityIspInfo(String countryNmae, String regionName, String cityName, String districtName, String networkService) {
        this.countryNmae = countryNmae;
        this.regionName = regionName;
        this.cityName = cityName;
        this.districtName = districtName;
        this.networkService = networkService;
    }

    public String getCountryNmae() {
        return countryNmae;
    }

    public void setCountryNmae(String countryNmae) {
        this.countryNmae = countryNmae;
    }

    public String getRegionName() {
        return regionName;
    }

    public void setRegionName(String regionName) {
        this.regionName = regionName;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getDistrictName() {
        return districtName;
    }

    public void setDistrictName(String districtName) {
        this.districtName = districtName;
    }

    public String getNetworkService() {
        return networkService;
    }

    public void setNetworkService(String networkService) {
        this.networkService = networkService;
    }

    public final static CityIspInfo getCityInfo(String ip){
        CityIspInfo cityIspInfo = null;
        try {
            String[] cns = district.find(ip,"CN");
            if (cns.length==5){
                cityIspInfo = new CityIspInfo(cns[0],cns[1],cns[2],cns[3],cns[4]);
            } else{
                cityIspInfo = new CityIspInfo();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return cityIspInfo;
    }
}
